#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<unistd.h>
#include<sys/types.h>
#include<sys/wait.h>
#include<assert.h>

#define LEFT "["
#define RIGHT "]"
#define LABLE "#"
#define DELIM " \t"

#define LINE_SIZE 1024
#define ARGC_SIZE 100
#define EXIT_CODE 44

char pwd[LINE_SIZE];
char command_line[LINE_SIZE];
char* _argv[ARGC_SIZE];


const char* get_user_name()
{
    return getenv("USER");
}

const char* get_host_name()
{
    return getenv("HOSTNAME");
}

const char* getpwd()
{
    return getcwd(pwd, sizeof(pwd));
}

void interact(char* cline, int size)
{
    getpwd();
    printf(LEFT"主机名：%s""@""用户名：%s"" ""路径：%s"RIGHT LABLE" ", get_host_name(), get_user_name(), pwd);
    char* ptr = fgets(cline, size-1, stdin); //成功则返回对应字符串，不成功返回空
    assert(ptr);
    cline[strlen(cline)-1] = '\0'; //strlen返回结果不含\0,但fgets末尾是"\n\0",所以要去掉或替换掉\n
    printf("%s\n", command_line);

}

int split_string(char* cline, char* argv[])
{

    int i = 0;
    argv[i++] = strtok(cline, DELIM);
    while(argv[i++] = strtok(NULL, DELIM));

    // 测试用
    //int j = 0;
    //for(;argv[j];j++)
    //{
    //    printf("%s\n", argv[j]);
    //}
    //printf("num = %d\n", i-1);
    
    return i-1;
}

void normal_execute(char* argv[])
{
    pid_t id  = fork();
    if(id < 0)
    {
        perror("fork fail!");
        return;
    }
    else if(id == 0)
    {
        execvp(argv[0], argv);
        exit(EXIT_CODE);
    }
    else
    {
        int status = 0;
        pid_t ret = waitpid(id, &status, 0);
        if(ret == id)
        {
            int last_code = WEXITSTATUS(status);
            if(!last_code)
                printf("等待成功\n");
            else
            {
                perror("等待失败\n");
                return;
            }       
        }

    }
}

int main()
{
    while(1)
    {
        interact(command_line, sizeof(command_line));
        split_string(command_line, _argv);
        normal_execute(_argv);
    }


    return 0;
}
